$(function() {
        // Initializes and creates emoji set from sprite sheet
        window.emojiPicker = new EmojiPicker({
            emojiable_selector: '[data-emojiable=true]',
            assetsPath: '/addons/emoji-picker/lib/img',
            popupButtonClasses: 'fa fa-smile-o'
        });
        // Finds all elements with `emojiable_selector` and converts them to rich emoji input fields
        // You may want to delay this step if you have dynamically created input fields that appear later in the loading process
        // It can be called as many times as necessary; previously converted input fields will not be converted again
        window.emojiPicker.discover();
    });
    $(document).on('click','.second-comment-response',function(){
        var num =$(this).closest('.cimo-comment-right').attr('data-commentid');
        var uid =$(this).attr('data-uid');
        $('#commentform input[name=to_userid]').val(uid);
        $('#commentform input[name=cmtid]').val(num);
        if($(this).attr('data-uname')) {
            var toname = '回复' + $(this).attr('data-uname');
            $('#commentform span.toname').html(toname);
        }
        if(!$(this).hasClass('click')) {
            $('.second-comment-response').removeClass('click');
            $(this).addClass('click');
            $('#commentform').show();
            $('#commentform').insertAfter($('#comment-reply' + num));
        }else{
            var ishidden = $('#commentform').css('display');
            if (ishidden == "none") {
                $('#commentform').show();
            }else{
                $('#commentform').hide();
            }
        }
    });

    //点赞
    $(document).on('click','.comment-thumb',function(){
        var value = $(this).attr('data-id');
        var nn = parseInt($(this).find('span').html());
        var nh = $(this).find('span');
        var type =  $('input[name=comment_typeid]').val();
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '/comment/thumb?cid='+value,
            data: 'typeid='+type,
            dataType: 'json',
            success: function (data) {
                if (data.status == 'success') {
                    var jsonData = data.data;
                    var html = '';
                    if (data.total == 1) {
                        layer.msg('点赞成功');
                    } else if (data.total > 1) {
                        nn  +=1;
                        nh.html(nn);
                        layer.msg(data.msg);
                    }
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg('点赞成功');
            }
        });
    });

    //一级回复
    $(document).on('click','#submit-main',function(){
        var comment_typeid =   $('input[name=comment_typeid]').val();
        var url= '/comment/push?typeid='+comment_typeid;
        var comentapp =   $('input[name=commentapp]').val();
        var comentappid =   $('input[name=commentappid]').val();
        var token =  $('input[name=_token]').val();
        cimo_first_comment(url,comentapp,comentappid,token);
    });

    //举报
    $(document).on('click','.comment-tipoff',function(){
        var tid = $(this).attr('data-id');
        $('input[name=tipoffid]').val(tid);
        $('#examModalcm').modal();
    });

    $(document).on('click','.comment-tipoffs',function(){
        var value =  $('input[name=tipoffid]').val();
        var reason = $('select[name=reason]').val();
        var type =  $('input[name=comment_typeid]').val();
        $('#examModalcm').modal('hide');
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '/comment/tipoff?cid='+value+'&reason='+reason,
            data: 'typeid='+type,
            dataType: 'json',
            success: function (data) {
                if (data.status == 'success') {
                    var jsonData = data.data;
                    var html = '';
                    if (data.total == 1) {
                        layer.msg('举报成功，谢谢');
                    } else if (data.total > 1) {
                        layer.msg(data.msg);
                    }
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg('举报成功，谢谢');
            }
        });
    });

$(document).on('click','.secondsubmit',function(){
    var comment_typeid =   $('input[name=comment_typeid]').val();
    var url= '/comment/push?typeid='+comment_typeid;
    var cmtid =  $('input[name=cmtid]').val();
    var cuid =  $('input[name=to_userid]').val();
    var comentapp =   $('input[name=commentapp]').val();
    var comentappid =   $('input[name=commentappid]').val();
    var token =  $('input[name=_token]').val();
    cimo_second_comment(url, cmtid, cuid, comentapp, comentappid, token);
   
});

function cimo_second_comment(url, comid, uid, app, appid, token) {
    var content = $(".comment-reply-content-common").val();
    $.ajax({
        type: 'post',
        cache: 'false',
        url: url,
        data: 'cid=' + comid + '&content=' + content + '&touid=' + uid + '&app=' + app + '&appid=' + appid + '&_token=' + token,
        dataType: 'json',
        success: function (data) {
            if (data.status == 'success') {
                var jsonData = data.data;
                var html = '';
                if (data.total == 1) {
                    html += '<div class="comment-caret-up"><i class="glyphicon glyphicon-triangle-top"></i></div>';
                    html += '<div class="comment-child-content" id="comment-child-content' + comid + '">';
                    html += '<div class="comment-child-content-list">';
                    html += '<p class="fs14 color-green"><span>' + jsonData.username +'</span><span class="ml-10" style="margin-right: 10px;">回复</span><span>'
                        + jsonData.tousername +'：</span>'+'<span class="ml-20">'
                        +jsonData.created_at+'</span>'
                        +'<span style="float:right;">'
                        +'<a class="comment-tipoff" data-id="'+jsonData.comment_id+'">举报</a>'
                        +'<a class="ml-10 comment-thumb" data-id="'+jsonData.comment_id+'">点赞(<span>0</span>)</a>'
                        +'<a class="ml-20 second-comment-response" data-uname="'+jsonData.username+'" data-uid="'+jsonData.user_id+'" data-id="'+jsonData.comment_id+'">回复</a>'
                        +'</span>'
                        + '</p>';
                    html += '<p class="fs14 c5" >' + jsonData.content + '</p>';
                    html += '</div>';
                    html += '</div>';
                    $("#comment-childs" + comid).append(html);
                } else if (data.total > 1) {
                    html += '<div class="comment-child-content-list" style="">';
                    html += '<p class="fs14 color-green"><span>' + jsonData.username +'</span><span class="ml-10" style="margin-right: 10px;">回复</span><span>'
                        + jsonData.tousername +'：</span>'+'<span class="ml-20">'
                        +jsonData.created_at+'</span>'
                        +'<span style="float:right;">'
                        +'<a class="comment-tipoff" data-id="'+jsonData.comment_id+'">举报</a>'
                        +'<a class="ml-10 comment-thumb" data-id="'+jsonData.comment_id+'">点赞(<span>0</span>)</a>'
                        +'<a class="ml-20 second-comment-response" data-uname="'+jsonData.username+'" data-uid="'+jsonData.user_id+'" data-id="'+jsonData.comment_id+'">回复</a>'
                        +'</span>'
                        + '</p>';
                    html += '<p class="fs14 c5">' + jsonData.content + '</p>';
                    html += '</div>';
                    $("#comment-child-content" + comid).append(html);
                }
                $("#message" + comid).show().html("发表成功！").fadeOut(2000);
                $("#comment-reply" + comid).hide();
                $(".comment-reply-content-common").val("");
                $(".comment-reply-content-common").next('div').html("");
                $("input[name=totalnum" + comid + "]").val(data.total);
                $("#comment-reply-content" + comid).val("");
            } else {
                $("#message" + comid).show().html(data.msg).fadeOut(2000);
            }
        },
        error: function () {
            $("#message" + comid).show().html("发表失败！").fadeOut(2000);
        }
    });
}


    //一级评论
    function cimo_first_comment(url, app, appid, token) {
        var content = $("#comment-reply-content").val();
        $.ajax({
            type: 'post',
            cache: 'false',
            url: url,
            data: 'content=' + content + '&app=' + app + '&appid=' + appid + '&_token=' + token,
            dataType: 'json',
            success: function (data) {
                if (data.status == 'success') {
                    var jsonData = data.data;
                    var html = '';
                    html += '<div class="cimo-comment-common" >';
                    html += '<div class="cimo-comment-user-avatar fl ">';
                    html += '<img src="' + jsonData.logo + '">';
                    html += '</div>';
                    html += '<div class="fl cimo-comment-right" data-commentid="'+jsonData.comment_id+'">';
                    html += '<div class="comment-content text-left" style="">';
                    html += '<p class="cimo-comment-first"><span class="cimo-comment-username">' + jsonData.username + '</span>'
                          + '<span class="ml-10 cimo-comment-time">'+jsonData.created_at +'</span>'
                          + '<span style="float:right;"><a class="comment-tipoff" data-id="'+jsonData.comment_id+'">举报</a>'
                          + '<a class="ml-10 comment-thumb" data-id="'+jsonData.comment_id+'">点赞(0)</a> </span>'
                          + '</p>';
                    html += '<p class="cimo-comment-second" >' + jsonData.content + '</p>';
                    html += '<div class="fs14 c9 ma0">';
                    html += '<div class="fl">';
                    html += '<a href="javascript:;" class="second-comment-response" data-uid="'+jsonData.user_id+'">回复</a>';
                    html += '</div>';
                    html += '<div class="fr mr15">';
                    html += '<img src="' + jsonData.replyimg + '">&nbsp;';
                    html += '<input type="text" name="totalnum' + jsonData.comment_id + '" value="0" readonly="readonly">';
                    html += '</div>';
                    html += '<div class="clr"></div>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="comment-child text-left" id="comment-childs' + jsonData.comment_id + '">';
                    html += '</div>';
                    html += '<div id="message' + jsonData.comment_id + '" class="commentmsg dn"></div>';
                    html += '<div id="comment-reply'+jsonData.comment_id+'" class="comment-reply-form" style=" border: none;"></div>';
                    html += '</div>';
                    html += '<div class="clr"></div>';
                    html += '</div>';
                    $("#cimo-comment-container").prepend(html);
                    $("#message").show().html("发表成功！").fadeOut(2000);
                    $("#comment-reply-content").val("");
                    $("#comment-reply-content").next('div').html("");
                    $('#holder').show();
                    jPages();
                } else {
                    $("#message").show().html(data.msg).fadeOut(5000);
                }
            },
            error: function () {
                $("#message").show().html("发表失败！").fadeOut(2000);
            }
        });
    }



function cjPages() {
    $("div.holder").jPages({
        containerID: "cimo-comment-container",
        previous: "上一页",
        next: "下一页",
        perPage: 4,
        animation: 'fadeInRight'
    });
    var loc = window.location.hash;
    if(loc !=''&& loc !=undefined){
        loc = parseInt(loc.substr(1));
        var locpage = $('a[name='+loc+']').attr('cdata-id');
        locpage = parseInt(locpage);
        locpage = Math.ceil(locpage/4);
        $(".holder").jPages(locpage);
    }
}